<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('blog_model');
    }

    public function index()
	{
        /*
         *  分页五个要素：总条数，当前页码，每页条数 =>起始行数，总页数
         *
         *  对于分页类来说只是制造页码链接，他只需要知道总页数和当前页就能就行了，
         *  所以一共提供给分页类：当前页,总条数,每页条数就行了，而总页数分页类内部会通过总条数和每页条数计算出来
         *
         *  而对于渲染该页的数据来说，只需要提供limit后的两个参数给sql方法，即起始行数和每页条数即可：
         *  其中起始行数通过当前页码，每页条数自行计算出来。 总条数总页数无所谓
         */
        $perPage = 2;
        $pageNow = $this->uri->segment(3) ? $this->uri->segment(3) : 1;
        $firstRow = ($pageNow - 1) * $perPage;
        $data['blogs'] = $this->blog_model->getBlogNowPage($perPage, $firstRow);

        //$totalPages = ceil($count/$perPage);
        $this->load->library('pagination');    //加载分页类
        $config['use_page_numbers'] = TRUE;    //开启传递当前选中的页码，TRUE让分页类认为url的传参是当前页码
        $config['num_links'] = 2;   //显示的页码数目
        $config['base_url'] =site_url('home/index');    //地址路径
        $config['total_rows'] = $this->blog_model->getTotal();    //总条数
        $config['per_page'] = $perPage;    //每页显示数量，默认显示10条

        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"> <a href=#> ';
        $config['cur_tag_close'] = '</a></li>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open']  = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['prev_link'] = '<span aria-hidden="true">上一页</span>';
        $config['next_link'] = '<span aria-hidden="true">下一页</span>';

        $this->pagination->initialize($config);    //加载配置信息
        $data['page'] = $this->pagination->create_links();    //要显示到界面的分页信息

		$this->load->view('index' , $data);
	}

	public function show($id)
    {
        $data['id'] = intval($id);
        $this->load->view('show', $data);
    }
}